package com.xiaoyu.client.ui.activity.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xiaoyu.client.R;
import com.xiaoyu.client.ui.activity.main.mine.my_order.OrderFragment;
import com.xiaoyu.client.ui.fragment.home_search.CommunityFragment;
import com.xiaoyu.client.ui.fragment.home_search.GoodsFragment;
import com.xiaoyu.client.ui.fragment.home_search.HelpFragment;
import com.xiaoyu.client.ui.fragment.home_search.MerchantsFragment;
import com.xiaoyu.client.ui.fragment.home_search.NearbyShopFragment;
import com.xiaoyu.client.ui.fragment.home_search.UserFragment;
import com.xiaoyu.client.ui.fragment.main.HomeFragment;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页搜索
 */
public class HomeSearchActivity extends BaseActivityWithTitle {

    private View mView;
    @BindView(R.id.home_search_linear)
    LinearLayout mBarLinear;
    @BindView(R.id.home_search_line)
    View mBarline;
    @BindView(R.id.home_search_viewpager)
    ViewPager mViewPager;
    private CommonFragmentPagerAdapter mPageAdapter;
    private ViewPagerUtils mPagerUtils;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText(getIntent().getStringExtra(HomeFragment.HOME_SEARCH));
        mView = LayoutInflater.from(this).inflate(R.layout.activity_home_search, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < mBarLinear.getChildCount(); i++) {
            mBarLinear.getChildAt(i).setOnClickListener(this);
        }

        mPagerUtils = new ViewPagerUtils(this, mBarLinear, mBarline);
        mPagerUtils.initLine();
        mPagerUtils.setTextColor("#333333", "#0c93e9");
        mPagerUtils.selectItem(0);

        fragments.clear();
        fragments.add(new NearbyShopFragment(getIntent().getStringExtra(HomeFragment.HOME_SEARCH)));//附近商家
        fragments.add(new HelpFragment());
        fragments.add(new MerchantsFragment());
        fragments.add(new GoodsFragment(getIntent().getStringExtra(HomeFragment.HOME_SEARCH)));//商品
        fragments.add(new UserFragment());
        fragments.add(new CommunityFragment());

        mPageAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mBarLinear.getChildAt(0)) {

            mPagerUtils.selectItem(0);
            mPagerUtils.scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);
        }
        if (v == mBarLinear.getChildAt(1)) {

            mPagerUtils.selectItem(1);
            mPagerUtils.scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);
        }
        if (mBarLinear.getChildAt(2) == v) {

            mPagerUtils.selectItem(2);
            mPagerUtils.scrollLineMove(2, 0);
            mViewPager.setCurrentItem(2);
        }
        if (mBarLinear.getChildAt(3) == v) {

            mPagerUtils.selectItem(3);
            mPagerUtils.scrollLineMove(3, 0);
            mViewPager.setCurrentItem(3);
        }
        if (mBarLinear.getChildAt(4) == v) {

            mPagerUtils.selectItem(4);
            mPagerUtils.scrollLineMove(4, 0);
            mViewPager.setCurrentItem(4);
        }
        if (mBarLinear.getChildAt(5) == v) {
            mPagerUtils.selectItem(5);
            mPagerUtils.scrollLineMove(5, 0);
            mViewPager.setCurrentItem(5);
        }
    }

    /**
     * viewPager切换监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mPagerUtils.scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mPagerUtils.selectItem(position);
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}
